//
//  TransferViewElement.m
//  SolarSeek
//
//  Created by Erick Fejta on 05/04/23.
//  Copyright 2005 Erick Fejta. All rights reserved.
//

#import "TransferViewElement.h"
#import "Transfers.h"
#import "Shares.h"
#import "Peers.h"
#import "ApplicationDefaults.h"

@implementation TransferViewElement

- (NSFont *)font {
	return [NSFont systemFontOfSize:[[NSUserDefaults standardUserDefaults] floatForKey:SolarSeekDefaultSizeFontTransferName]];
}

- (id)initWithTransfer:(Transfer *)transfer {
	if(self = [super init]) {
		_transfer = [transfer retain];
		NSMutableParagraphStyle *mps = [[[NSMutableParagraphStyle alloc] init] autorelease];
		[mps setAlignment:NSLeftTextAlignment];
		[mps setLineBreakMode:NSLineBreakByTruncatingMiddle];
		//top line
		_name = [[NSMutableAttributedString alloc] initWithString:[[[_transfer share] filenameSharedUnix] lastPathComponent]
			attributes:[NSDictionary dictionaryWithObjectsAndKeys:
				[self font],NSFontAttributeName,
				mps,NSParagraphStyleAttributeName,nil]
			];
		//second line
		NSAttributedString *as = [[[NSAttributedString alloc] initWithString:[@"\n" stringByAppendingString:[[_transfer peer] name]] 
			attributes:[NSDictionary dictionaryWithObjectsAndKeys:
//					[self font],NSFontAttributeName,
					[NSColor grayColor],NSForegroundColorAttributeName,
					mps,NSParagraphStyleAttributeName,nil]
			] autorelease];
		[_name appendAttributedString:as];
	}
	return self;
}

- (void)dealloc {
	[_transfer release];
	[_name release];
	[super dealloc];
}

#pragma mark KVO

- (NSImage *)icon {
	return [[_transfer share] icon];
}

- (NSAttributedString *)name {
	return _name;
}

- (void)setState:(int)s { state = s; }
- (int)state { return state; }

- (NSAttributedString *)transfer {
	NSMutableParagraphStyle *mps = [[[NSMutableParagraphStyle alloc] init] autorelease];
	[mps setAlignment:NSCenterTextAlignment];
	[mps setLineBreakMode:NSLineBreakByTruncatingTail];
	NSValueTransformer *speedvt = [NSValueTransformer valueTransformerForName:@"SolarSeekValueTransformerSpeed"];
	NSMutableAttributedString *t = [[NSMutableAttributedString alloc] init];
	if([_transfer state]==TransferStateTransferring) {
		NSAttributedString *at = [[[NSAttributedString alloc] initWithString:[speedvt transformedValue:[NSNumber numberWithDouble:[_transfer speed]]]
			attributes:[NSDictionary dictionaryWithObjectsAndKeys:mps,NSParagraphStyleAttributeName,nil]] autorelease];
		[t appendAttributedString:at];
	}
//	else if([_transfer state]==TransferStateFinished) {
//		NSAttributedString *at = [[[NSAttributedString alloc] initWithString:@"Complete"
//			attributes:[NSDictionary dictionaryWithObjectsAndKeys:mps,NSParagraphStyleAttributeName,nil]] autorelease];
//		[t appendAttributedString:at];
//	}
	
	NSValueTransformer *sizevt = [NSValueTransformer valueTransformerForName:@"SolarSeekValueTransformerSize"];
	NSAttributedString *at2 = [[[NSAttributedString alloc] initWithString:[@"\n" stringByAppendingFormat:@"%@ / %@",
				[sizevt transformedValue:[NSNumber numberWithUnsignedLongLong:[_transfer countBytesTransferred]]],
				[sizevt transformedValue:[NSNumber numberWithUnsignedLongLong:[_transfer countBytesTotal]]]]
				attributes:[NSDictionary dictionaryWithObjectsAndKeys:mps,NSParagraphStyleAttributeName,[NSColor darkGrayColor],NSForegroundColorAttributeName,nil]] autorelease];
	[t appendAttributedString:at2];
	return [t autorelease];
}

- (NSNumber *)percentageComplete {
	double done = [_transfer countBytesTransferred];
	double tot = [_transfer countBytesTotal];
	TransferState localstate = [_transfer state];
	if(localstate==TransferStateFailed) {
		done = done==0?-1.0:done*-1.0;
	}
	else if(localstate==TransferStateFinished)
		done = 1.0;
	else if(tot != 0)
		done /= tot;
	return [NSNumber numberWithDouble:done];
}

- (NSAttributedString *)time {
	NSMutableParagraphStyle *mps = [[[NSMutableParagraphStyle alloc] init] autorelease];
	[mps setAlignment:NSCenterTextAlignment];
	[mps setLineBreakMode:NSLineBreakByTruncatingTail];

	NSString *first=nil, *second=nil;
	switch([_transfer state]) {
		case TransferStateStarting:
			first = @"Starting";
			second = [_transfer descriptionState];
			break;
		case TransferStateIdle: {
			first = [_transfer descriptionState];
			unsigned location = [_transfer locationQueue];
			if(location != 0) {
				NSString *num = [[NSValueTransformer valueTransformerForName:@"SolarSeekValueTransformerCardinalNumbers"] transformedValue:[NSNumber numberWithUnsignedInt:location]];
				second = [NSString stringWithFormat:@"%@ in Queue",num];
			}
			break;
		}
		case TransferStateTransferring: {
			first = [[NSValueTransformer valueTransformerForName:@"SolarSeekValueTransformerTime"] transformedValue:[NSNumber numberWithDouble:[_transfer timeIntervalRemaining]]];
			second = @"remaining";
			break;
		}
		case TransferStateFailed:
			first = @"Stopped";
			second = [_transfer descriptionState];
			if([second isEqual:first])
				second = @"";
			break;
	}
	NSMutableAttributedString *t = [[NSMutableAttributedString alloc] initWithString:first?first:@""
		attributes:[NSDictionary dictionaryWithObjectsAndKeys:mps,NSParagraphStyleAttributeName,nil]];
	if(second) {
		[t appendAttributedString:[[[NSAttributedString alloc] initWithString:[@"\n" stringByAppendingString:second]
			attributes:[NSDictionary dictionaryWithObjectsAndKeys:[NSColor darkGrayColor],NSForegroundColorAttributeName,mps,NSParagraphStyleAttributeName,nil]] autorelease]];
	}
	return [t autorelease];
}

- (id)data { return _transfer; }

@end
